<template>
  <div>
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        <el-button size="small" justify @click="hCancel">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { addRole, updateRole } from '@/api/setting'

export default {
  name: 'VueAdminTemplateMasterRoledia',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
        //   console.log(this.row)
        // 展开运算符
        //   this.form = { ...this.row }
        // JSON.parse
          this.roleForm = JSON.parse(JSON.stringify(this.row))
        //   console.log(this.form)
        }
      },
      immediate: true
    }
  },

  mounted() {

  },

  methods: {
    hSubmit() {
      this.$refs.roleForm.validate(async valid => {
        if (!valid) return this.$message.error('请正确填写')
        let res = {}
        if (this.isEdit) {
          res = await updateRole(this.roleForm)
        } else {
          res = await addRole(this.roleForm)
        }

        // console.log(res)
        if (res.code !== 10000) return
        this.$emit('success')
        this.$emit('update:visible', false)
        this.$refs.roleForm.resetFields()
      })
    },
    hCancel() {
      this.$emit('update:visible', false)
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
